package com.zs.backend.modules.enterpriseOrder.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zs.backend.common.enumeration.OrderAuditingStatus;
import com.zs.backend.common.enumeration.OrderLogType;
import com.zs.backend.common.utils.CollectionUtil;
import com.zs.backend.modules.enterpriseOrder.dto.EnterpriseOrderDTO;
import com.zs.backend.modules.enterpriseOrder.mapper.EnterpriseOrderMapper;
import com.zs.backend.modules.enterpriseOrder.model.EnterpriseOrder;
import com.zs.backend.common.UserHolder;
import com.zs.backend.common.utils.BeanUtil;
import com.zs.backend.common.utils.StringUtil;
import com.zs.backend.modules.enterpriseOrder.service.EnterpriseOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zs.backend.modules.mobileManagement.dto.MobileManagementDTO;
import com.zs.backend.modules.orderLog.dto.OrderLogDTO;
import com.zs.backend.modules.orderLog.service.OrderLogService;
import com.zs.backend.modules.ums.model.UmsAdmin;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

/**
 * <p>
 * 企业订单管理表 服务实现类
 * </p>
 *
 * @author zs
 * @since 2024-03-18
 */
@Service
public class EnterpriseOrderServiceImpl extends
    ServiceImpl<EnterpriseOrderMapper, EnterpriseOrder> implements EnterpriseOrderService {

    @Override
    public Page<EnterpriseOrderDTO> list(EnterpriseOrderDTO dto, Integer pageSize, Integer pageNum) {
        Page<EnterpriseOrder> page = new Page<>(pageNum,pageSize);
        QueryWrapper<EnterpriseOrder> wrapper = new QueryWrapper<>();
        LambdaQueryWrapper<EnterpriseOrder> lambda = wrapper.lambda();
        lambda.likeRight(StringUtil.isNotEmpty(dto.getEnterprise()),
            EnterpriseOrder::getEnterprise, dto.getEnterprise());
        lambda.likeRight(StringUtil.isNotEmpty(dto.getPersonnel()),
            EnterpriseOrder::getPersonnel, dto.getPersonnel());

        lambda.eq(dto.getType() != null,
            EnterpriseOrder::getType, dto.getType());
        lambda.likeRight(StringUtil.isNotEmpty(dto.getMajor()),
            EnterpriseOrder::getMajor, dto.getMajor());
        lambda.likeRight(StringUtil.isNotEmpty(dto.getDestination()),
            EnterpriseOrder::getDestination, dto.getDestination());
        lambda.likeRight(StringUtil.isNotEmpty(dto.getCompanyName()),
            EnterpriseOrder::getCompanyName, dto.getCompanyName());

        lambda.likeRight(StringUtil.isNotEmpty(dto.getPersonnelName()),
            EnterpriseOrder::getPersonnelName, dto.getPersonnelName());

        lambda.ge(dto.getStartContractEndTime() != null,
            EnterpriseOrder::getContractEndTime, dto.getStartContractEndTime());

        lambda.le(dto.getEndContractEndTime() != null,
            EnterpriseOrder::getContractEndTime, dto.getEndContractEndTime());

        lambda.orderByDesc(EnterpriseOrder::getUpdateTime);
        Page<EnterpriseOrder> pageList = page(page,wrapper);
        if(pageList == null){
            return new Page<>();
        }
        List<EnterpriseOrderDTO> result = BeanUtil.beanCopyPropertiesForList(pageList.getRecords(), EnterpriseOrderDTO.class);

        if(CollectionUtil.isNotNullAndEmpty(result)){

            for(EnterpriseOrderDTO enterpriseOrderDTO : result){
                if(StringUtil.isNotEmpty(enterpriseOrderDTO.getContactTel())){
                    enterpriseOrderDTO.setEncodeTel(StringUtil.encryptPhoneNumber(enterpriseOrderDTO.getContactTel()));
                }
            }
        }
        Page<EnterpriseOrderDTO> pageResult = new Page<>(page.getCurrent(), page.getSize(), page.getTotal());
        pageResult.setRecords(result);
        return pageResult;
    }

    @Override
    public Long addOrUpt(EnterpriseOrderDTO request) {
        UmsAdmin umsAdmin = UserHolder.getCurrentAdmin();
        request.setCreateUid(umsAdmin.getId());
        request.setCreateUname(umsAdmin.getUsername());
        if(request.getPaymentImageFile() != null){
            request.setPaymentImageFileData(getPaymentImageFileData(request));
        }
        EnterpriseOrder enterpriseOrder = BeanUtil.beanCopyProperties(request, EnterpriseOrder.class);

        if(request.getId() != null){
            updateById(enterpriseOrder);
            return request.getId();
        }
        this.save(enterpriseOrder);
        return enterpriseOrder.getId();

    }

    @Override
    public List<EnterpriseOrder> getOrderNoExpires() {
        QueryWrapper<EnterpriseOrder> wrapper = new QueryWrapper<>();
        wrapper.select("id", "contract_end_time");
        LambdaQueryWrapper<EnterpriseOrder> lambda = wrapper.lambda();
        lambda.eq(EnterpriseOrder::getExpire, false);
        List<EnterpriseOrder> list = this.list(lambda);
        return list;
    }

    private byte[] getPaymentImageFileData(EnterpriseOrderDTO request){
        MultipartFile file = request.getPaymentImageFile();
        byte[] paymentImageFileData = null;
        if(file == null){
            return paymentImageFileData;
        }
        try {
            paymentImageFileData = file.getBytes();
            String fileName = file.getOriginalFilename();
            request.setPaymentImageFileName(fileName);
            request.setPaymentImageFileType(fileName.substring(fileName.lastIndexOf(".") + 1));
        }catch (Exception e){
            throw new RuntimeException("解析文件异常");
        }

        return paymentImageFileData;

    }


}
